package com.example.activities;

import BDD.Queries;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.view.MotionEvent;
import android.view.View;
import android.widget.*;
import com.example.R;


import java.io.File;
import java.io.IOException;


public class EditPhoto extends Activity {

    public File myDirectory = new File(Environment.getExternalStorageDirectory() + "/AppImages/");
    public File photo;
    public EditText placeName;
    public EditText contact;
    boolean editMode;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editphoto);
        int position = this.getIntent().getExtras().getInt("position");
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        photo = myDirectory.listFiles()[position];
        editMode = true;
        placeName = (EditText) findViewById(R.id.place);
        contact = (EditText) findViewById(R.id.personInformations);
        Cursor c = Queries.getPicture(photo.getAbsolutePath());
        if (c.getCount() != 0) {
            editMode = false;
            c.moveToFirst();
            placeName.setText(c.getString(1));
            contact.setText(c.getString(2));
            c.close();
        }

        imageView.setImageDrawable(Drawable.createFromPath(photo.getAbsolutePath()));
        imageView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {

                return true;
            }
        });
    }

    public void save(View view) {
        EditText infoPerson = (EditText) findViewById(R.id.personInformations);
        EditText infoPlace = (EditText) findViewById(R.id.place);
        String img = photo.getAbsolutePath();
        Queries.checkMode(editMode, img, infoPerson.getText().toString(), infoPlace.getText().toString());
        Intent intent = new Intent(EditPhoto.this, MainGallery.class);
        startActivity(intent);

    }

    public void locateButtonClick(View view) throws IOException {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null) {
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
        }
    }

    public void addButtonClick(View view) {
        Intent contactInsertIntent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(contactInsertIntent, 1);
    }

    public void contactButtonClick(View view) {
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(contactPickerIntent, 1);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}, null, null, null);
            startManagingCursor(cursor);
            if (cursor.moveToFirst()) {
                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                    EditText editText = (EditText) findViewById(R.id.personInformations);
                    editText.setText(editText.getText() + name + ",");
                }
            }
        } else {
            Toast.makeText(this, "Error in having contact", Toast.LENGTH_LONG);
        }
    }

}